﻿
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using Button = System.Windows.Controls.Button;

namespace Report.Forms
{
    /// <summary>
    /// FrmRemoveCurve.xaml 的交互逻辑
    /// </summary>
    public partial class FrmRemoveCurve : Window
    {
        private readonly ReportManager1 _manager;
        private readonly ObservableCollection<CurveListItem> _curves;
        private bool _isRemove;
        public FrmRemoveCurve()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
            _manager = ReportManager1.Create();
            _curves = new ObservableCollection<CurveListItem>();
        }

        private void FrmRemoveCurve_OnLoaded(object sender, RoutedEventArgs e)
        {
            _initCurves();
        }

        private void _initCurves()
        {
            _curves.Clear();
            foreach (var curve in _manager.Curves)
            {
                var cli = new CurveListItem
                {
                    Name = curve.Title,
                    IsShowPoint = curve.IsDrawPoint
                };
                var fd = _manager.DataInfo.SingleOrDefault(d => d.DataNo == curve.X.Id);
                if (fd != null)
                {
                    cli.XData = fd.Text;
                }
                var str = new StringBuilder();
                foreach (var ys in curve.Y)
                {
                    fd = _manager.DataInfo.SingleOrDefault(d => d.DataNo == ys.Id);
                    if (fd != null)
                    {
                        str.Append(fd.Text).Append(",");
                    }
                }
                cli.YDatas = str.Remove(str.Length - 1, 1).ToString();
                _curves.Add(cli);
            }
            Curves.ItemsSource = _curves;
        }

        private void BtnRemove_OnClick(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            var title = btn?.Tag.ToString();
            if (_manager.RemoveCurve(title) > 0)
            {
                _isRemove = true;
                var cli = _curves.SingleOrDefault(c => c.Name == title);
                if (cli != null) _curves.Remove(cli);
            }
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            DialogResult = _isRemove;
            Close();
        }

        private void Curves_OnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            //e.Row.Height = 40;
            ////粗略计算行高。为了更好的显示效果
            //var info = (CurveListItem)e.Row.DataContext;
            //if (info != null)
            //{
            //    //计算最大长度的文本
            //    string maxLengthString = info.YDatas;
            //    //获取换行文本的文本框宽度，即template里面的textbox或textblock的实际宽度
            //    double textBoxWidth = Curves.Columns[3].ActualWidth;

            //    var formattedText = new FormattedText(maxLengthString, CultureInfo.CurrentUICulture, System.Windows.FlowDirection.LeftToRight,
            //        new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
            //        20, Brushes.Black, null, TextFormattingMode.Display, 1.25);

            //    double calculateHeight = (formattedText.Height * (formattedText.Width / textBoxWidth));
            //    e.Row.Height = 40 > calculateHeight ? 40 : 40 * 2;
            //}
        }
    }

    public class CurveListItem
    {
        public string Name { get; set; }
        public bool IsShowPoint { get; set; }
        public string XData { get; set; }
        public string YDatas { get; set; }
    }

}
